package org.nanhill.xmlro.srojs;

import java.util.Iterator;
import java.util.Map;

import org.nanhill.xmlro.Serializer;
import org.nanhill.xmlro.XmlroException;
import org.nanhill.xmlro.XmlroOutput;

/**
 * Serializing a java.util.Map.
 * 
 * only Map<String, Object>
 * 
 * @author alin
 * 
 */
public class JavascriptMapSerializer implements Serializer {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.xmlro.model.Serializer#writeObject(java.lang.Object,
	 *      org.nanhill.xmlro.XmlroOutput)
	 */
	public void writeObject(Object obj, XmlroOutput out) throws XmlroException {
		Map map = (Map) obj;
		out.writeMapBegin();
		for (Iterator itt = map.keySet().iterator(); itt.hasNext();) {
			Object key = itt.next();
			// only write string key
			out.writeMapKey((String) key);
			out.writeObject(map.get(key));
			if (itt.hasNext()) {
				out.writeMapElementSplit();
			}
		}
		out.writeMapEnd();
	}
}
